/** 
 * Project Name:ZhiWeiCateringManagementSystem 
 * File Name:DishOrderRecordServers.java 
 * Package Name:cn.ccit.servers 
 * Date:2016年11月5日下午8:35:36 
 * Copyright (c) 2016, 120026139@qq.com All Rights Reserved. 
 * 
*/  
  
package cn.ccit.servers;  

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.ccit.DAO.DishOrderRecordDAO;
import cn.ccit.DAOImp.DishOrderRecordDAOimp;
import cn.ccit.base.BaseConstants;
import cn.ccit.entites.DiningTable;
import cn.ccit.entites.record.DishOrderRecord;
import cn.ccit.entites.record.DishOrderRecordItem;
import cn.ccit.entites.record.PayOrderRecord;
import cn.ccit.entites.record.PayOrderRecordItem;
import cn.ccit.iservers.IDiningTableServers;
import cn.ccit.iservers.IDishOrderRecordItemServers;
import cn.ccit.iservers.IDishOrderRecordServers;
import cn.ccit.iservers.IPayOrderRecordItemServers;
import cn.ccit.iservers.IPayOrderRecordServers;
import cn.ccit.util.ConstantsUtil;
import cn.ccit.util.GsonUtil;
import cn.ccit.util.MyDBUtil;

/** 
 * ClassName:DishOrderRecordServers <br/> 
 * Function: TODO ADD FUNCTION. <br/> 
 * Reason:   TODO ADD REASON. <br/> 
 * Date:     2016年11月5日 下午8:35:36 <br/> 
 * @author   Administrator 
 * @version   
 * @since    JDK 1.6 
 * @see       
 */
public class DishOrderRecordServers implements IDishOrderRecordServers {
	private DishOrderRecordDAO orderRecordDao =null;
	private IDishOrderRecordItemServers idishItemServers=null;
	private Map<String, String> map=null;
	private IPayOrderRecordItemServers iPayItemServers=null;
	private IPayOrderRecordServers iPayRecordServers =null;
	private IDiningTableServers idt=null;
	private IDiningTableServers idts=null;
	public DishOrderRecordServers() {
		orderRecordDao =new DishOrderRecordDAOimp();
		idishItemServers= new DishOrderRecordItemServers();
		iPayItemServers =new PayOrderRecordItemServers();
		iPayRecordServers =new PayOrderRecordServers();
		idts = new DiningTableServers();
		idt=new DiningTableServers();
	}
	@Override
	public boolean addDishOrderRecord(DishOrderRecord doRecord) {
		doRecord=this.setNowTime(doRecord);
		doRecord.setName("菜品订单");
		doRecord.setEnumType(BaseConstants.PAYSTATE);
		doRecord.setEnumName(BaseConstants.NOPAY);
		doRecord.setEnumCode(BaseConstants.PayState.NOPAY.ordinal());
		return orderRecordDao.addDishOrderRecord(doRecord);
	}

	@Override
	public boolean removeDishOrderRecord(DishOrderRecord doRecord) {
		return orderRecordDao.removeDishOrderRecord(doRecord);
	}

	@Override
	public boolean changeDishOrderRecordInfo(DishOrderRecord doRecord) {
		return orderRecordDao.changeDishOrderRecordInfo(doRecord);
	}

	@Override
	public List<DishOrderRecord> getSomeDishOrderRecords(int start, int limit) {
		return orderRecordDao.getSomeDishOrderRecords(start, limit);
	}

	@Override
	public DishOrderRecord searchDishOrderRecordByID(int id) {
		return orderRecordDao.searchDishOrderRecordByID(id);
	}

	@Override
	public List<DishOrderRecord> searchDishOrderRecordByYear(String year) {
		return orderRecordDao.searchDishOrderRecordByYear(year);
	}

	@Override
	public List<DishOrderRecord> searchDishOrderRecordByMonth(String month) {
		return orderRecordDao.searchDishOrderRecordByMonth(month);
	}

	@Override
	public List<DishOrderRecord> searchDishOrderRecordByDate(String date) {
		return orderRecordDao.searchDishOrderRecordByDate(date);
	}
	
	@Override
	public Long orderDish(DishOrderRecord doRecord,
			String itemList) {
		doRecord.setEnumType(BaseConstants.PAYSTATE);
		doRecord.setEnumName(BaseConstants.NOPAY);
		doRecord.setEnumCode(BaseConstants.PayState.NOPAY.ordinal());
		DishOrderRecord dor=this.setNowTime(doRecord);
		dor.setSerialsNumber(
				ConstantsUtil.getSerialsNumber(doRecord.getDiningTableID(), 
						doRecord.getTid()));
//		List<DishOrderRecordItem> list =(List<DishOrderRecordItem>) GsonUtil.
//				GsonToList(itemList,DishOrderRecordItem.class); 
		List<DishOrderRecordItem> list =GsonUtil.stringToArray(itemList, 
				DishOrderRecordItem[].class);
		list=this.setItemListData(list, dor);
		if(idishItemServers.batchAddDishOrderRecordItem(list)){
			this.addDishOrderRecord(dor);
			DiningTable dt=idts.searchDiningTableByID(dor.getDiningTableID());
			dt.setSerialsNumber(dor.getSerialsNumber());
			idts.changeDiningTableInfo(dt);
			return dor.getSerialsNumber();
		}
		return null;
	}
	private DishOrderRecord setNowTime(DishOrderRecord doRecord) {
		map=ConstantsUtil.getNowTimeMap();
		doRecord.setCreatTime(map.get(BaseConstants.CRTEAT_TIME));
		doRecord.setOrderDate(map.get(BaseConstants.DATE));
		doRecord.setOrderMonth(map.get(BaseConstants.MONTH));
		doRecord.setOrderYear(map.get(BaseConstants.YEAR));
		doRecord.setLastModfiyTime(map.get(BaseConstants.CRTEAT_TIME));
		doRecord.setLastModfiyUid(doRecord.getCreatUid());
		doRecord.setLastModfiyUname(doRecord.getCreatUname());
		doRecord.setName("菜品订单");
		return doRecord;
	}
	private List<DishOrderRecordItem> setItemListData(List<DishOrderRecordItem> list,
			DishOrderRecord doRecord) {
		for (DishOrderRecordItem item : list) {
			item.setName("点菜明细");
			item.setCreatTime(doRecord.getCreatTime());
			item.setCreatUid(doRecord.getCreatUid());
			item.setCreatUname(doRecord.getCreatUname());
			item.setEnumType(BaseConstants.DISHSTATE);
			item.setEnumCode(BaseConstants.DishState.NoServeDish.ordinal());
			item.setEnumName(BaseConstants.NOSERVEDISH);
			item.setDiningTableID(doRecord.getDiningTableID());
			item.setDiningTableName(doRecord.getDiningTableName());
			item.setLastModfiyTime(doRecord.getCreatTime());
			item.setLastModfiyUid(doRecord.getCreatUid());
			item.setLastModfiyUname(doRecord.getCreatUname());
			item.setOrderYear(doRecord.getOrderYear());
			item.setOrderMonth(doRecord.getOrderMonth());
			item.setOrderDate(doRecord.getOrderDate());
			item.setTid(doRecord.getTid());
			item.setTname(doRecord.getTname());
			item.setSerialsNumber(doRecord.getSerialsNumber());
		}
		
		return list;
	}
	@Override
	public List<DishOrderRecord> searchDishOrderRecordByTid(DishOrderRecord doRecord) {
		return orderRecordDao.searchDishOrderRecordByTid(doRecord.getDiningTableID());
	}
	@Override
	public List<DishOrderRecord> searchDishOrderRecordBySerialsNumber(
			Long serialsNumber) {
		return orderRecordDao.searchDishOrderRecordBySerialsNumber(serialsNumber);
	}
	@Override
	public boolean payForOrder(Long serialsNumber) {
		PayOrderRecord payRecord= PayOrderRecord.
				getPayOrderFromDishOrder(
						orderRecordDao.
						searchDishOrderRecordBySerialsNumber(serialsNumber).get(0)
						);
		List<PayOrderRecordItem> itemlist=this.orderItem2payItem(
				idishItemServers.searchDishOrderRecordItemBySerialsNumber(serialsNumber));		
		
		if(payRecord!=null&&itemlist!=null){
			if(iPayItemServers.batchAddPayOrderRecordItem(itemlist)&&
					iPayRecordServers.addPayOrderRecord(payRecord)
					){
				MyDBUtil.removeAllDataFromTable("DishOrderRecordItem","serialsNumber",String.valueOf(serialsNumber));
				MyDBUtil.removeAllDataFromTable("DishOrderRecord","serialsNumber",String.valueOf(serialsNumber));
				DiningTable tempDt=new DiningTable();
				tempDt.setId(payRecord.getDiningTableID());
				tempDt.setEnumCode(BaseConstants.TableState.CloseTable.ordinal());
				tempDt.setEnumName(BaseConstants.CLOSETABLE);
				idt.changeDiningTableState(tempDt);
				return true;
			}
		}
		return false;
	}
	
	private List<PayOrderRecordItem> orderItem2payItem
				(List<DishOrderRecordItem> list) {
		List<PayOrderRecordItem> temp=new ArrayList<PayOrderRecordItem>();
		PayOrderRecordItem payTemp;
		for (int i = 0; i < list.size(); i++) {
			payTemp=PayOrderRecordItem.getPayOrderItemFromDishOrderItem(list.get(i));
			temp.add(payTemp);
			payTemp=null;
		}
		return temp.size()>0?temp:null;
	}
}
